<?php

namespace app\app\controller;

use app\app\model\ProjectGGKTypeModel;
use app\app\model\ProjectModel;
use app\BaseController;
use app\app\model\ProjectGGKModel;

class ProjectManage extends BaseController
{

    public function index()
    {
        $project_id = input('project_id');
        $list = ProjectGGKModel::field('a.id as project_ggk_id,a.project_id,a.state,a.finsh_time,b.id,b.parent_id,b.name')
            ->where('a.project_id', $project_id)->alias('a')
            ->join('t_project_gkk_type b', 'a.ggk_id=b.id')->order('a.ggk_id asc')->select()->toArray();
        $last = array(
            ['id' => '1000', 'parent_id' => '0', 'state' => '未完成', 'finsh_time' => '', 'name' => '六、项目关闭'],
            ['id' => '1001', 'parent_id' => '1000', 'state' => '未完成', 'finsh_time' => '', 'name' => '1.项目关闭'],
            ['id' => '1002', 'parent_id' => '1001', 'state' => '未完成', 'finsh_time' => '', 'name' => '1.1项目关闭']
        );
        $list = array_merge($list, $last);
        if ($list) {
            $temp = [];
            foreach ($list as $v) {
                $v['children'] = [];
                $temp[$v['id']] = $v;
            }
            foreach ($temp as $k => $v) {
                $temp[$v['parent_id']]['children'][] = &$temp[$v['id']];
            }
            $this->success('success', $temp[0]);
        }
        $this->error('error', '项目不存在');

    }


    public function add()
    {
        $project_id = input('project_id');
        $control_type_info = ProjectGGKTypeModel::select()->toArray();

        $todo_task_list = array();
        foreach ($control_type_info as $key => $value) {
            $item['project_id'] = $project_id;
            $item['ggk_id'] = $value['id'];
            $item['create_time'] = date('Y-m-d H:i:s', time());
            array_push($todo_task_list, $item);
        }
        $res = (new \app\app\model\ProjectGGKModel)->saveAll($todo_task_list, false);
        $this->success('success');
    }


    public function typeAdd()
    {
        $data = $this->request->param();
        ProjectGGKTypeModel::create($data);
        $this->success('success');
    }

    public function typeList()
    {
        $list = ProjectGGKTypeModel::select()->toArray();
        $this->success('success', $list);
    }


    public function typeSearch()
    {
        $type = input('type', '');
        $project_id = input('project_id', '');
        $name = input('name', '');
        $where = [];
        $where[] = ['b.type', '=', $type];
        $where[] = ['a.project_id', '=', $project_id];
        if ($name) $where[] = ['b.nick_name', 'like', '%' . $name . '%'];
        $list = ProjectGGKModel::alias('a')
            ->field('a.id,b.nick_name as name')
            ->where($where)
            ->leftJoin('t_project_gkk_type b', 'a.ggk_id = b.id')
            ->select();
        $this->success('success', $list);
    }


    public function projecAddGgk()
    {
        $project = ProjectModel::where('is_parent', '1')->select();

        $control_type = ProjectGGKTypeModel::select();

        $todo_task_list = array();
        foreach ($project as $k => $v) {
            foreach ($control_type as $key => $value) {

                $item['project_id'] = $v['id'];
                $item['ggk_id'] = $value['id'];
                $item['create_time'] = date('Y-m-d h:i:s', time());
                array_push($todo_task_list, $item);
            }

        }
        $res = (new \app\app\model\ProjectGGKModel)->saveAll($todo_task_list, false);
        $this->success('success');

    }


}
